#include "stdafx.h"
#include <iostream>
#include <vector>
#include <assert.h>
using namespace std;

class doubleList {
private:
	vector <double> *dList;
public:
	doubleList() { dList = new vector<double>(); }
	~doubleList() { delete dList; }
	void insert();
	void substractList( int, double );
	void reverseList();
	void printList();
};

void doubleList::insert() {
	dList->push_back( 3.1415 );
	dList->push_back( 4.1415 );
	dList->push_back( 5.1415 );
	dList->push_back( 6.1415 );
	dList->push_back( 3.1415 );
	dList->push_back( 14.241415 );
	dList->push_back( 6.9875 );
	dList->push_back( 10.1415 );
	dList->push_back( 4.00324 );
	dList->push_back( 10.8685 );
	dList->push_back( 16.14155 );
}

void doubleList::printList() {
	cout << "OUTPUT: ";
	for ( int i = 0 ; i < dList->size() ;i ++ ) 
		cout << (*dList)[i] << " ";
	cout << endl << endl;

	return;
}

void doubleList::substractList( int position, double value ) {
	vector <double> *tempVector;
	tempVector = new vector<double>;
	char buffer[64];
	int expectedLength;

	if ( position >= dList->size() ) {
		cerr << "Position exceeds list size" << endl;
		return;
	}
	if ( position < 0 ) {
		cerr << "Negative positions not accepted" << endl;
		return;
	}

	expectedLength = dList->size();
	for ( int i = position ; i < dList->size() ; i++ ) {
		(*dList)[i]  = (*dList)[i] - value;	 
	}
	for ( int i = 0 ; i < dList->size() ; i++ ) {
		char is_zero = 1;
		sprintf( buffer, "%f", (*dList)[i] );
		for ( int j = 0 ; j < strlen( buffer ) ; j++ ) 
			if ( buffer[j] != '0' && buffer[j] != '-' && buffer[j] != '.' ) {
				is_zero = 0;

		}
		if ( !is_zero ) 
			tempVector->push_back(  (*dList)[i] );
		else 
			expectedLength--;
	}
	
	assert( tempVector->size() == expectedLength );
	delete this->dList;
	dList = tempVector;

	return;
}

void doubleList::reverseList() {
	vector <double> *tempVector;
	tempVector = new vector<double>;
	
	for( int i = dList->size() - 1 ; i >= 0 ; i--  ) {
		tempVector->push_back( dList->at(i) );
	}
	assert( dList->size() == tempVector->size() );
	delete this->dList;
	dList = tempVector;
	
	return;
}

int main( int argc, char* argv[] ) {
	doubleList *test = new doubleList();
	test->insert();
	test->printList();
	test->reverseList();
	test->printList();
	test->substractList( 0, 3.1415 );
	test->printList();
	test->substractList( 0, 3.8460 );
	test->printList();
	test->substractList( 3, 9.154050 );
	test->printList();
	test->reverseList();
	test->printList();
	test->substractList( 4, -9.58730 );
	test->printList();
	system( "pause" );

	return 0;
}